import Vue from 'vue'
import Vuex from 'vuex'

import {
  tbRoleQuery,
  tbRoleAdd,
  tbRoleUpdate,
  tbSysRoleMenuAddBatch,
  tbRoleDelete,
  tbRoleCopy,
} from '@/api/admin/user/tbSysRole'

Vue.use(Vuex)

const state = {
  roleList: [],
}

const getters = {}

const actions = {
  async queryTbRoleList({ commit }, listQuery) {
    let res = await tbRoleQuery({ ...listQuery })
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async copyTbRole({ commit }, row) {
    let res = await tbRoleCopy(row.id)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async addTbRole({ commit }, data) {
    let res = await tbRoleAdd(data)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async updateTbRole({ commit }, data) {
    let res = await tbRoleUpdate(data)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  async deleteTbRole({ commit }, row) {
    let res = await tbRoleDelete(row.id)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
  //配置角色菜单
  async menuAddBatchTbSysRole({ commit }, roleId) {
    let res = await tbSysRoleMenuAddBatch(roleId)
    return res.data.code == 0 ? res.data : Promise.reject(new Error('faile'))
  },
}

const mutations = {
  SET_Role: (state, d) => {
    state.roleList = d
  },
}

export default {
  state,
  getters,
  actions,
  mutations,
}
